﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BudgetMealsModels.Entities;
using BudgetMealsModels.Abstract;
using System.IO;
using System.Data.Entity.Validation;
using System.Data;
using System.Data.SqlClient;

namespace BudgetMealsModels.Concrete
{
    public class EFDbRecipeRepository : IRecipeRepository
    {
        private EFDbContext _context = EFDbContext.Context;

        public IQueryable<Recipe> Recipes
        {
            get { return _context.Recipes.Include("Comments").Include("Ingredients").Include("ImageGallery"); }
        }

        public IQueryable<User> Users
        {
            get { return _context.Users.Include("Recipes"); }
            
        }
        public int Edit(Recipe recipe, int userId)
        {
            User u = _context.Users.FirstOrDefault(cu => cu.UserID == userId);
            var r = _context.Recipes.Find(recipe.RecipeID);
            int idChanged = recipe.RecipeID;
            recipe.UserCreatedID = userId;
            recipe.UserCreated = u.UserName;
            if (recipe.RecipeID == 0)
            {
                if (u.Recipes == null)
                    u.Recipes = new List<Recipe>();
                u.Recipes.Add(recipe);
                _context.Recipes.Add(recipe);
                _context.SaveChanges();
                idChanged = _context.Recipes.FirstOrDefault(x => x.Name == recipe.Name && x.ServingSize == recipe.ServingSize && x.Summary == recipe.Summary && x.Instructions == recipe.Instructions).RecipeID;
            }
            else
            {
                _context.Entry(_context.Users.SingleOrDefault(cu => cu.UserID == userId)).CurrentValues.SetValues(u);
                _context.Entry(r).CurrentValues.SetValues(recipe);
                /*using (var conn = new SqlConnection(_context.Database.Connection.ConnectionString))
                {
                    var deleteQuery = "DELETE FROM Images WHERE Recipe_RecipeID = " + recipe.RecipeID;
                    var command = new SqlCommand(deleteQuery, conn);
                    conn.Open();
                    command.ExecuteNonQuery();
                    conn.Close();
                    foreach (var i in recipe.ImageGallery)
                    {
                        if (i.ImageID == 0)
                        {
                            var query = "INSERT INTO Images (ImageData, ImageMimeType, Recipe_RecipeID) VALUES (@ImageData, @ImageMimeType, @Recipe_RecipeID)";
                            command = new SqlCommand(query, conn);
                            command.Parameters.AddWithValue("@ImageData", i.ImageData);
                            command.Parameters.AddWithValue("@ImageMimeType", i.ImageMimeType);
                            command.Parameters.AddWithValue("@Recipe_RecipeID", recipe.RecipeID);
                            conn.Open();
                            command.ExecuteNonQuery();
                            conn.Close();
                        }
                    }
                }*/
            }
            try
            {
                _context.SaveChanges();
            }
            catch (DbEntityValidationException e)
            {
                foreach (var er in e.EntityValidationErrors)
                {
                    foreach (var y in er.ValidationErrors)
                    { }
                }

            }
            return idChanged;
                
        }
        public void Delete(Recipe recipe, int userId)
        {
            User u = _context.Users.FirstOrDefault(cu => cu.UserID == userId);
            if (recipe != null)
            {

                u.Recipes.Remove(recipe);
                //Recipe fix = _context.Recipes.FirstOrDefault(x => x.RecipeID == recipe.RecipeID);
                
            }
            _context.SaveChanges();
        }

        public void AddComment(Comment comment)
        {
            Recipe recipe = _context.Recipes.FirstOrDefault(x => x.RecipeID == comment.Recipe.RecipeID);
            User u = _context.Users.FirstOrDefault(cu => cu.UserID == recipe.UserCreatedID);
            if (recipe != null)
            {
                if (comment.RecipeRating != 0)
                {
                    if (recipe.Comments == null)
                    {
                        recipe.Comments = new List<Comment>();
                    }

                    recipe.Comments.Add(comment);
                    
                    _context.Entry(_context.Recipes.SingleOrDefault(r => r.RecipeID == recipe.RecipeID)).CurrentValues.SetValues(recipe);
                }
            }
            _context.SaveChanges();
        }
    }
}
